package com.chenwut.annotation;

import java.lang.reflect.Method;

/**
 * @author ChenPi
 * @since 2024/09/24 16:09
 */
public class AnnotationDemo2 {
    public static void main(String[] args) {
        // 目标： 搞清楚注解的应用场景：模拟Junit框架，有MyTest注解的方法，就执行，否则不执行
        Class clazz = AnnotationDemo2.class;
        Method[] methods = clazz.getDeclaredMethods();

        AnnotationDemo2 demo2 = new AnnotationDemo2();
        for (Method method : methods) {
            // 1.判断方法上是否有MyTest注解
            if (method.isAnnotationPresent(MyTest.class)) {
                MyTest annotation = method.getDeclaredAnnotation(MyTest.class);
                int count = annotation.count();
                // 2.执行方法
                try {
                    for (int i = 0; i < count; i++) {
                        method.invoke(demo2);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }

    @MyTest
    public void test1() {
        System.out.println("方法一执行了");
    }

    @MyTest(count = 3)
    public void test2() {
        System.out.println("方法二执行了");
    }

    public void test3() {
        System.out.println("方法三执行了");
    }
}
